//
//  CalculatorViewController.m
//  Calculator
//
//  Created by Willem van Willigen on 06/11/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CalculatorViewController.h"
#import "GraphViewController.h";

@interface CalculatorViewController()
@property (readonly) CalculatorBrain *brain; //parentheses magic, to have private property! readonly --> only getter
@end

@implementation CalculatorViewController

@synthesize display;

- (void)viewDidLoad {
	self.title = @"Calculator";
	self.contentSizeForViewInPopover = [self boundingBox];
	CGRect bounds = self.view.bounds;
	NSLog(@"calc viewDidLoad. frame: %g %g %g %g", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
}

- (CalculatorBrain *)brain {
	if(!brain) {
		brain = [[CalculatorBrain alloc] init];
	}
	return brain;
}

- (GraphViewController *)graphViewController {
	if(!graphViewController) {
		NSLog(@"graphVC getter");
		graphViewController = [[GraphViewController alloc] init];
	}
	return graphViewController;
}

#define MARGIN 20;
- (CGSize) boundingBox {
	NSLog(@"boudingBox called");
	CGRect result = CGRectZero;
	for (UIView *v in self.view.subviews) {
		result = CGRectUnion(result, v.frame);
	}
	result.size.height += MARGIN;
	result.size.width += MARGIN;
	return CGSizeMake(result.size.width, result.size.height);
}

- (IBAction) digitPressed:(UIButton *)sender {
	NSString *digit = sender.titleLabel.text;
	if (userIsInTheMiddleOfTypingANumber) {
		self.display.text = [self.display.text stringByAppendingString:digit];
	} else {
		self.display.text = digit;
		userIsInTheMiddleOfTypingANumber = YES;
	}
}

- (IBAction) operationPressed:(UIButton *)sender {
	if(userIsInTheMiddleOfTypingANumber) {
		self.brain.operand = [self.display.text doubleValue];
		userIsInTheMiddleOfTypingANumber = NO;
	}
	NSString *operation = sender.titleLabel.text;
	[self.brain performOperation:operation];
	if ([CalculatorBrain variablesInExpression:self.brain.expression] && ![sender.titleLabel.text isEqual:@"Solve!"]) {
		self.display.text = [CalculatorBrain descriptionOfExpression:self.brain.expression];
	} else {
		self.display.text = [NSString stringWithFormat:@"%g", self.brain.operand];
	}
}

- (IBAction) dotPressed:(UIButton *)sender {
	if (userIsInTheMiddleOfTypingANumber) {
		if ([self.display.text rangeOfString:@"."].location == NSNotFound) {
			self.display.text = [self.display.text stringByAppendingString:@"."];
		}
	} else {
		self.display.text = @"0.";
		userIsInTheMiddleOfTypingANumber = YES;
	}
	
}

- (IBAction) varPressed:(UIButton *)sender {
	[self.brain setVariableAsOperand:sender.titleLabel.text];
	self.display.text = [CalculatorBrain descriptionOfExpression:self.brain.expression];
}

- (IBAction) graphPressed:(UIButton *)sender {
	//GraphViewController *gvc = [[GraphViewController alloc] init];
	if([self.brain.expression count] && ![[self.brain.expression objectAtIndex:[self.brain.expression count]-1] isEqual:@"="]) {
		[self.brain performOperation:@"="];
	}
	
	self.graphViewController.expression = self.brain.expression;
	
	self.graphViewController.title = [CalculatorBrain descriptionOfExpression:self.brain.expression];
	NSLog(@"before the push");
	if(self.graphViewController.view.window == nil) {
		if (self.graphViewController.parentViewController == nil) {
			NSLog(@"ahaaa");
		}
		[self.navigationController pushViewController:self.graphViewController animated:YES];
		[self.graphViewController updateView];
		
		NSLog(@"push it!");
	}
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
	//if ([self.parentViewController.parentViewController class] != [UISplitViewController class]) {
	//	return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
	//} else {
		return YES;
	//}
}

- (void) viewDidUnload {
	NSLog(@"cvc viewDidUnload");
	//[graphViewController.navigationController popViewControllerAnimated:NO];
}


- (void) dealloc {
	NSLog(@"calculatorviewcontroller dealloc");
	[brain release];
	[display release];
	//[graphViewController.navigationController setNavigationBarHidden:NO animated:YES];
	[graphViewController release];
	[super dealloc];
}

@end
